package servlets;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import actionForms.searchContactValidationForm;
import domain.Contact;
import domain.IDAOContact;

public class searchContactAction extends Action {

	public ActionForward execute(final ActionMapping pMapping, ActionForm pForm, final HttpServletRequest pRequest, final HttpServletResponse pResponse) {
		final searchContactValidationForm lForm = (searchContactValidationForm) pForm;
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(pRequest.getSession().getServletContext());
		IDAOContact dao = (IDAOContact)context.getBean("daoContact");	
		List<Contact> contacts = new ArrayList<Contact>();
		String searchBy = lForm.getSearchBy();
		String searchValue = lForm.getSearchValue();
		if(searchBy.equals("firstname")){
			contacts = dao.getContactByFirstName(searchValue);			
		} else if(searchBy.equals("lastname")){
			contacts = dao.getContactByLastName(searchValue);			
		} else if(searchBy.equals("email")){
			contacts = dao.getContactByEmail(searchValue);			
		}
		pRequest.setAttribute("contacts", contacts);
		return pMapping.findForward("resultsPage");
	}
}